package com.example.hyznbk.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hyznbk.MyApplication;
import com.example.hyznbk.R;
import com.example.hyznbk.model.result.FavouriteListEntity;
import com.example.hyznbk.model.result.LearnTypeEntity;
import com.example.hyznbk.presenter.FavouriteListPresenter;
import com.example.hyznbk.view.adapter.FavouriteListAdapter;
import com.example.hyznbk.view.adapter.PopupMenuType1Adapter;
import com.example.hyznbk.view.base.BaseMainFragment;
import com.example.hyznbk.view.view_interface.IFavouriteListView;
import com.example.hyznbk.view.widget.CustomFooterView;
import com.example.hyznbk.view.widget.PopupMenu;
import com.example.hyznbk.view.widget.SearchEditText;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class FavouriteListFragment extends BaseMainFragment<FavouriteListPresenter> implements IFavouriteListView ,View.OnClickListener{

    private SearchEditText etSearch;
    private FavouriteListAdapter mAdapter;
    private TextView mTvNumber;
    private TextView mTvLearnType;
    private PopupMenuType1Adapter mLearnTypeAdapter;
    private Map<String,String> mLeanTypeMap;
    private PopupMenu mPopupMenu;
    private View mMask;
    private RelativeLayout mSpLayout;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_favourite_list;
    }

    @Override
    public void initBaseMainViews(View v, Bundle savedInstanceState) {

        mMask = v.findViewById(R.id.bg_masker);
        mSpLayout = (RelativeLayout) v.findViewById(R.id.rel_toolbar_head);
        /*View toolBarLayout = v.findViewById(R.id.rel_toolbar_layout);

        toolBarLayout.findViewById(R.id.img_main_menu).setVisibility(View.GONE);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) toolBarLayout.getLayoutParams();
        layoutParams.setMargins(0,getStatusBarHeight(),0,0);

        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.favourite_list);*/

        Toolbar toolBar = (Toolbar) v.findViewById(R.id.toolbar);
        initToolBar(toolBar,true,R.string.favourite_list);
        mTvNumber = (TextView) v.findViewById(R.id.tv_paperNumber);
        mTvLearnType = (TextView)v.findViewById(R.id.sp_status);
        mTvLearnType.setOnClickListener(this);
        List<LearnTypeEntity> learnTypeEntities = ((MyApplication) getActivity().getApplication()).getLearnTypeEntities();
        String[] learnTypeNames = new String[learnTypeEntities.size()];
        mLeanTypeMap = new HashMap<>();
        for (int i = 0 ; i < learnTypeEntities.size();i++){
            LearnTypeEntity learnTypeEntity = learnTypeEntities.get(i);
            mLeanTypeMap.put(learnTypeEntity.getTypeName(),learnTypeEntity.getTypeID());
            learnTypeNames[i] = learnTypeEntity.getTypeName();
        }
        mLearnTypeAdapter = new PopupMenuType1Adapter(getActivity(),learnTypeNames);

        mPopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        mPopupMenu.setListAdapter(mLearnTypeAdapter);
        mPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvLearnType.setText(mPopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.searchFavouriteItem(etSearch.getText().toString(),mLeanTypeMap.get(mPopupMenu.getCheckedStr()));
            }
        });
        mTvLearnType.setText(mPopupMenu.getCheckedStr());
        mPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mMask.setVisibility(View.GONE);
                mTvLearnType.setSelected(false);
            }
        });

        etSearch = (SearchEditText) v.findViewById(R.id.et_search);
        etSearch.setOnSearchClickListener(new SearchEditText.OnSearchClickListener() {
            @Override
            public void onSearchClick(View view, String keyword) {
                mPresenter.searchFavouriteItem(etSearch.getText().toString(),mLeanTypeMap.get(mPopupMenu.getCheckedStr()));

            }
        });

        mAdapter = new FavouriteListAdapter(getHolderActivity());
        mAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new FavouriteListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                mPresenter.openFavoriteItemDetail(position, mAdapter.getAdapterItemList().get(position));
            }

            @Override
            public void onItemDel(View view, int position) {
                mPresenter.delItemFavourite(position,mAdapter.getAdapterItemList().get(position));
            }
        });
    }

    @Override
    protected void updateViews(boolean isShowLoading) {
        mPresenter = new FavouriteListPresenter(this);
        mPresenter.getData(isShowLoading);
    }


    @Override
    public void loadData(FavouriteListEntity data) {
        if (mRecyclerView.getLayoutManager() != null){
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }
       setQuesTotal(data.getTotalCount());
        mAdapter.clearAndAdd(data.getModelList());
        loadComplete(mAdapter.getAdapterItemCount() >= data.getTotalCount());
    }

    @Override
    public void loadMoreData(FavouriteListEntity data) {
        setQuesTotal(data.getTotalCount());
        mAdapter.add(data.getModelList());
        loadComplete(mAdapter.getAdapterItemCount() >= data.getTotalCount());
    }

    @Override
    public void delFavouriteSucceed() {
        mAdapter.remove();
        setQuesTotal(mAdapter.getAdapterItemCount());
        if (mAdapter.getAdapterItemCount() == 0){
            showNoData(getString(R.string.no_favourite_list));
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sp_status:
                mMask.setVisibility(View.VISIBLE);//阴影布局可见
                mTvLearnType.setSelected(true);
                mPopupMenu.showLocation(mSpLayout, 0, 0);
                break;
        }
    }


    @Override
    public void setQuesTotal(int number) {
        String paperTotal = String.format(getString(R.string.quesTotal),number);
        mTvNumber.setText(Html.fromHtml(paperTotal));
    }
}
